import React, {Component} from 'react'
import {fetchData} from '../../../components/AjaxConsumer'
import {
    Form,
    Input,
    Tooltip,
    Icon,
    Cascader,
    Select,
    Row,
    Col,
    Checkbox,
    Button,
    message,
    Modal,
    Radio
} from 'antd';
import { SelectUrl } from '../../../components';
const FormItem = Form.Item;

class WorkAreaForm extends React.Component {
    state = {
        loading: false,
        isEdit: false,
        submitUrl: '',
        whCodeUrl: '/base/data/warehouse/r/' + this.props.factoryCode + '/warehouse',
        regionCodeUrl: '',
        activeFlagUrl:'/base/dic/get/active_flag',
        formData: {}
    };

    onWhCodeChange = (whCode) => {
        if (whCode != '' && whCode != undefined) {
            this.setState({
                regionCodeUrl: '/base/region/r/' + whCode + '/region',
            });
        }
    }
    handleSubmit = (e) => {
        e.preventDefault();
        this.props.form.validateFieldsAndScroll((err, fieldsValue) => {
            if (!err) {
                this.setState({loading: true});
                let values = {
                    ...fieldsValue,
                };
                console.log('表单提交数据-----------', values);
                //发送ajax 控制请求数据规则，无值的设为空
                for (let item in values) {
                    if (!values[item]) {
                        values[item] = ''
                    }
                }
                const options = {
                    url: this.state.submitUrl,
                    type: 'post',
                    condition: values
                };

                fetchData(
                    this,
                    options,
                    (res) => {
                        //成功回调函数
                        this.setState({
                            loading: false
                        });
                        if (res.code == '1') {
                            message.success('操作成功');

                            this.handleCancel();
                            this.props.refreshHandle();
                        } else {
                            //message.error(res.msg);
                        }

                    },
                    (res) => {
                        this.setState({
                            loading: false
                        });
                    }
                )
            }
        });
    }

    handleCancel = () => {
        this.props.onCancel();//123
        this.props.form.resetFields(); //初始化表单
        //初始化ajax加载过来的selectList
    }

    //propertity 修改时执行
    componentWillReceiveProps(newProps){
        this.setState({
            isEdit: newProps.isEdit,
            submitUrl: newProps.submitUrl,
            formData: newProps.formData
        });
    }

    // 组件参数实例化-仅一次
    componentWillMount(){
        this.setState({
            isEdit: this.props.isEdit,
            submitUrl: this.props.submitUrl,
            formData: this.props.formData
        });
    }

    render() {
        const {getFieldDecorator} = this.props.form;
        const formItemLayout = {
            labelCol: {
                xs: {span: 24},
                sm: {span: 6}
            },
            wrapperCol: {
                xs: {span: 24},
                sm: {span: 18}
            }
        };
        const tailFormItemLayout = {
            wrapperCol: {
                xs: {span: 24, offset: 0},
                sm: {span: 24, offset: 10}
            }
        };

        let objectInfo = this.state.formData;

        return (
            <div>
                <Form
                    onSubmit={this.handleSubmit}
                    style={{
                                backgroundColor: '#fff',
                                paddingTop: '20px',
                                paddingBottom: '20px'
                            }}>
                    <Row>
                        <Col span={0}>
                            <FormItem>
                                {getFieldDecorator('workareaId', {
                                    initialValue: objectInfo.workareaId ? objectInfo.workareaId : "",
                                    rules: [
                                        {}
                                    ]
                                })(<Input type="hidden"/>)}
                            </FormItem>
                        </Col>
                        <Col span={24}>
                            <FormItem {...formItemLayout} label="工作区编码">
                                {getFieldDecorator('workareaCode', {
                                    initialValue: objectInfo.workareaCode ? objectInfo.workareaCode : "",
                                    rules: [
                                        {
                                            required: true, whitespace: true, message: '请输入工作区编码'
                                        },{
                                            max:100,message: '输入内容应少于100个字符'
                                        },{
                                            pattern:/^[-a-zA-z0-9\u4E00-\u9FA5]*$/,message:'输入内容不合法'
                                        }
                                    ]
                                })(
                                    <Input placeholder="请输入" disabled={this.state.isEdit}/>
                                )}
                            </FormItem>
                        </Col>
                        <Col span={24}>
                            <FormItem {...formItemLayout} label="工作区名称">
                                {getFieldDecorator('workareaName', {
                                    initialValue: objectInfo.workareaName ? objectInfo.workareaName : "",
                                    rules: [
                                        {
                                            required: true, whitespace: true, message: '请输入工作区名称'
                                        },{
                                            max:100,message: '输入内容应少于100个字符'
                                        },{
                                            pattern:/^[a-zA-z0-9\u4E00-\u9FA5]*$/,message:'输入内容不合法'
                                        }
                                    ]
                                })(
                                    <Input placeholder="请输入"/>
                                )}
                            </FormItem>
                        </Col>
                        <Col span={24}>
                            <FormItem {...formItemLayout} label="仓库编码">
                                {getFieldDecorator('whCode', {
                                    initialValue: objectInfo.whCode ? objectInfo.whCode : "",
                                    rules: [
                                        {
                                            required: true, whitespace: true, message: '请选择仓库编码'
                                        }
                                    ]
                                })(
                                    <SelectUrl url={this.state.whCodeUrl} type="post" onChange={this.onWhCodeChange}
                                               dicCode="warehouseCode" dicName="warehouseName"/>
                                )}
                            </FormItem>
                        </Col>
                        <Col span={24}>
                            <FormItem {...formItemLayout} label="区域编码" hasFeedback>
                                {getFieldDecorator('regionCode', {
                                    initialValue: objectInfo.regionCode ? objectInfo.regionCode : "",
                                    rules: [
                                        {
                                            required: true, whitespace: true, message: '请选择区域编码'
                                        }
                                    ]
                                })(
                                    <SelectUrl url={this.state.regionCodeUrl} type="post" dicCode="regionCode"
                                               dicName="regionDesc"/>
                                )}
                            </FormItem>
                        </Col>
                        <Col span={24}>
                            <FormItem {...formItemLayout} label="激活标记" hasFeedback>
                                {getFieldDecorator('activeFlag', {
                                    initialValue: objectInfo.activeFlag ? objectInfo.activeFlag : "",
                                    rules: [
                                        {
                                            required: true, whitespace: true, message: '请选择激活标记'
                                        }
                                    ]
                                })(
                                    <SelectUrl url={this.state.activeFlagUrl} type="get" dicCode="dicCode"
                                               dicName="dicName"/>
                                )}
                            </FormItem>
                        </Col>
                        <Col span={24} style={{ textAlign: 'center',marginTop:'10px' }}>
                                <Button type="primary" htmlType="submit" size="large">
                                    {this.state.loading ? '提交中...' : '确定'}
                                </Button>
                                <Button type="default" style={{marginLeft: '50px'}} size="large" onClick={this.handleCancel}>
                                    取消
                                </Button>
                        </Col>
                    </Row>
                </Form>
            </div>
        );
    }
}

export default Form.create()(WorkAreaForm);
